
import java.io.*;

/*
ID: huangmingbo
LANG: JAVA
TASK: transform
*/
public class transform {

    public static void main(String[] args) throws IOException {
        try (
                BufferedReader reader = new BufferedReader(new InputStreamReader(transform.class.getResourceAsStream(transform.class.getSimpleName() + ".in")));
                PrintWriter writer = new PrintWriter(new FileWriter(transform.class.getSimpleName() + ".out"));
        ) {
            // read a sqaure N
            int N = Integer.parseInt(reader.readLine());
            char[][] s1 = readSquare(reader, N);
            char[][] s2 = readSquare(reader, N);

            CharSquare origin = CharSquare.builder().buildSquare(s1);
            CharSquare after = CharSquare.builder().buildSquare(s2);

            if (origin.rotate90().same(after)) {
                writer.println(1);
                return;
            }

            if (origin.rotate90().same(after)) {
                writer.println(2);
                return;
            }
            if (origin.rotate90().same(after)) {
                writer.println(3);
                return;
            }

            if (origin.buildSquare(s1).reflect().same(after)) {
                writer.println(4);
                return;
            }

            if (origin.rotate90().same(after)
                    || origin.rotate90().same(after)
                    || origin.rotate90().same(after)) {
                writer.println(5);
                return;
            }

            if (origin.buildSquare(s1).same(after)) {
                writer.println(6);
                return;
            }

            writer.println(7);

            /*
            #1: 90 Degree Rotation: The pattern was rotated clockwise 90 degrees.
            #2: 180 Degree Rotation: The pattern was rotated clockwise 180 degrees.
            #3: 270 Degree Rotation: The pattern was rotated clockwise 270 degrees.
            #4: Reflection: The pattern was reflected horizontally (turned into a mirror image of itself by reflecting around a vertical line in the middle of the image).
            #5: Combination: The pattern was reflected horizontally and then subjected to one of the rotations (#1-#3).
            #6: No Change: The original pattern was not changed.
            #7: Invalid Transformation: The new pattern was not obtained by any of the above methods.
             */

            // #6: No Change: The original pattern was not changed.
        }
    }

    private static char[][] readSquare(BufferedReader reader, int N) throws IOException {
        char[][] tmp = new char[N][N];
        String str;
        for (int i = 0; i < N; i++) {
            str = reader.readLine();
            for (int i1 = 0; i1 < N; i1++) {
                tmp[i][i1] = str.charAt(i1);
            }
        }

        return tmp;
    }


    private static class CharSquare {

        char[][] square;
        int len;

        private CharSquare() {

        }

        public static CharSquare builder() {
            return new CharSquare();
        }

        public CharSquare buildSquare(char[][] charArray) {

            this.len = charArray.length;
            this.square = new char[this.len][this.len];
            for (int i = 0; i < this.len; i++) {
                for (int i1 = 0; i1 < this.len; i1++) {
                    this.square[i][i1] = charArray[i][i1];
                }
            }

            return this;
        }


        public boolean same(CharSquare charSquare) {

            for (int i = 0; i < this.len; i++) {
                for (int i1 = 0; i1 < this.len; i1++) {
                    if (this.square[i][i1] != charSquare.square[i][i1]) {
                        return false;
                    }
                }
            }

            return true;

        }

        public CharSquare reflect() {
            char tmp;
            for (int i = 0; i < this.len; i++) {
                for (int i1 = 0; i1 < this.len / 2; i1++) {
                    tmp = square[i][i1];
                    square[i][i1] = square[i][this.len - 1 - i1];
                    square[i][this.len - 1 - i1] = tmp;
                }
            }

            return this;
        }

        public CharSquare rotate90() {

            char[][] tmp = new char[this.len][];
            for (int i = 0; i < this.len; i++) {
                tmp[i] = new char[this.len];
            }

            for (int i = 0; i < this.len; i++) {
                for (int i1 = this.len - 1; i1 >= 0; i1--) {
                    tmp[i][this.len - 1 - i1] = square[i1][i];
                }
            }

            this.square = tmp;
            return this;
        }

    }
}
